<?php
include_once 'Panel/pnlHeader.php'; 
include_once 'prepend.inc.php';

class PostLogin extends QForm
{
	protected $txtSearchTerm;
	protected $btnSearch;
	protected $dtgShareDetails;
	protected $pnlHeader;
	protected $dtrShareDetails;
	protected $arrShareDetails;
	
	protected function Form_Run()
	{
		QApplication::$LoadedPage = 'Home';
		if($_SESSION['User'] == '')
			QApplication::Redirect('index.php');
	}
	
	protected function Form_Create()
	{
		$this->txtSearchTerm_Create();
		$this->btnSearch_Create();
		$this->pnlHeader_Create();
		$this->dtrShareDetails_Create();
	}
	
	protected function dtrShareDetails_Create()
	{
		$this->dtrShareDetails = new QDataRepeater($this);
		$this->dtrShareDetails->Paginator = new QPaginator($this);
		$this->dtrShareDetails->ItemsPerPage = 10;

                
		$this->dtrShareDetails->UseAjax = false;

		$this->dtrShareDetails->Template = 'ShareDetails.tpl.php';
		$this->dtrShareDetails->SetDataBinder('dtrShareDetails_Bind');
	}
	
	protected function dtrShareDetails_Bind()
	{
		$condition = $this->GetShareDetails();
		$this->dtrShareDetails->TotalItemCount = Sharedetails::QueryCount($condition);
		$this->dtrShareDetails->DataSource = $this->arrShareDetails;
	}
	
	protected function txtSearchTerm_Create()
	{
			$this->txtSearchTerm = new QTextBox($this);	
			$this->txtSearchTerm->AddAction(new QEnterKeyEvent(),new QServerAction('btnSearch_Click'));	
	}
	
	protected function btnSearch_Create()
	{
		$this->btnSearch = new QButton($this);
		$this->btnSearch->Text = 'Search';
		$this->btnSearch->AddAction(new QClickEvent(),new QServerAction('btnSearch_Click'));
	}
	
	protected function btnSearch_Click()
	{
		$this->dtrShareDetails_Bind();
	}
	
	protected function GetShareDetails()
	{
		$objClauses = array();
		array_push($objClauses, QQ::OrderBy(QQN::Sharedetails()->ReturnDate));
		if ($objClause = $this->dtrShareDetails->LimitClause)
		{
			array_push($objClauses, $this->dtrShareDetails->LimitClause);
		}
		if($this->txtSearchTerm->Text == "")
		{
			$objCondition = QQ::Equal(QQN::Sharedetails()->Owner,$_SESSION['User']);
			$myassets = Sharedetails::QueryArray($objCondition,$objClauses);
		}
		else
		{
			$objCondition = QQ::AndCondition(
							QQ::OrCondition(
								QQ::Like(QQN::Sharedetails()->Asin,'%'. $this->txtSearchTerm->Text . '%'),
								QQ::Like(QQN::Sharedetails()->Email,'%'. $this->txtSearchTerm->Text . '%'),
								QQ::Like(QQN::Sharedetails()->TakenDate,'%'. $this->txtSearchTerm->Text . '%'),
								QQ::Like(QQN::Sharedetails()->ReturnDate,'%'. $this->txtSearchTerm->Text . '%'),
								QQ::Like(QQN::Sharedetails()->Title,'%'. $this->txtSearchTerm->Text . '%'),
								QQ::Like(QQN::Sharedetails()->FullName,'%'. $this->txtSearchTerm->Text . '%')
											),
								QQ::Equal(QQN::Myassets()->Owner,$_SESSION['User'])
								); 
			$objDbResult = Sharedetails::QueryArray($objCondition, $objClauses);
			$myassets = $objDbResult;
			
		}
		//$this->dtrShareDetails->TotalItemCount = Sharedetails::QueryCount($condition);
		$this->arrShareDetails = $myassets;
		return $objCondition;
	}

	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}

}

PostLogin::Run('PostLogin');
?>